#include "head.h"

void* thread_main(void* argv){

    status_t* status = (status_t*)argv;

    MYSQL* thread_mysql;
    if(!init_mysql(&thread_mysql)){
        ERROR_LOG("Error code: %d  ", ERROR1);
    }

    while(1){

        int net_fd = 0;

        pthread_mutex_lock(&status->lock);
        while(status->q.size <= 0){

            pthread_cond_wait(&status->cond, &status->lock);

        }

        net_fd = status->q.head->net_fd;
        dequeue(&status->q);

        pthread_mutex_unlock(&status->lock);

        int instruction = -1;

        recv(net_fd, &instruction, sizeof(int), 0);

        switch(instruction){

        case PUTS:
            printf("Instruction: puts\n");
            to_work_puts(net_fd, thread_mysql);
            printf("puts done\n");
            break;
        case GETS:
            printf("Instruction: gets\n");
            to_work_gets(net_fd, thread_mysql);
            printf("gets done\n");
            break;
        }

        //mysql_close(mysql_conn);
        
        close(net_fd);

    }

    return NULL;
}
